package com.elcl.widget.comp.edt;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.elcl.andbaselibrary.R;


/**
 * Created by lichunlin on 17/5/4.
 */

public class EditWithMaxNumber extends RelativeLayout {

    public EditWithMaxNumber(Context context) {
        super(context);
        initView(context);
    }

    private int maxNumber;

    public EditWithMaxNumber(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MaxNumber);
        maxNumber = ta.getInt(R.styleable.MaxNumber_max_number, 0);
        ta.recycle();

        initView(context);
    }

    public EditWithMaxNumber(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    private TextView tv_left_number;
    private EditText edt_input;

    private long showToastTime = 0;

    private void initView(Context context) {
        LayoutInflater lf = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        final View view = lf.inflate(
                R.layout.rel_input, null);

        edt_input = (EditText) view.findViewById(R.id.edt_input);
        tv_left_number = (TextView) view.findViewById(R.id.tv_left_number);
        tv_left_number.setText("" + maxNumber);

        edt_input.setFilters(new InputFilter[]{
                new InputFilter.LengthFilter(maxNumber)
        });

        addView(view, new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT));
        edt_input.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER && System.currentTimeMillis() - showToastTime > 1000) {
                    Toast.makeText(getContext(), "不支持换行", Toast.LENGTH_SHORT).show();
                    showToastTime = System.currentTimeMillis();
                }
                return (event.getKeyCode() == KeyEvent.KEYCODE_ENTER);
            }
        });
        edt_input.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String content = editable.toString();
                int length = content.length();
                int left = maxNumber - length;
                if (left < 0) {
                    left = 0;
                }
                tv_left_number.setText("" + left);
            }
        });
    }

    public EditText getInputEdit() {
        return edt_input;
    }


}
